<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" style="margin-bottom:30px">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="12" :sm="24">
            <a-button type="primary" icon="plus" @click="addIntoClick">新增角色</a-button>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-input-search placeholder="请输入类型关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :pagination="pagination"
    >
      <span slot="name" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="remark" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="createTime" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm
            title="删除不可恢复，请谨慎操作！！！"
            @confirm="handleSub(record)"
            @cancel="cancel"
            okText="确定"
            cancelText="取消">
            <a href="#">删除</a>
          </a-popconfirm>
        </template>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { roleList } from '@/api/system'
import utils from '@/utils/common'
import { mapActions } from 'vuex'
import moment from 'moment'
var timeStamp = Number(new Date())

export default {
  name: 'SystemRole',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      keyword: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: '角色名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '描述',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' }
        },
        {
          title: '时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return roleList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      keyword: this.keyword
    }
  },
  methods: {
    ...mapActions(['IntoUpdate']),
    moment,
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    addIntoClick () { // 新增角色
      this.$router.push('/system/role/add')
    },
    handleEdit (record) { // 编辑
      this.$router.push('/system/role/edit/' + record.id)
    },
    handleSub (record) { // 删除
      this.ApiUpdate(record)
    },
    cancel () {},
    ApiUpdate (record) { // 删除
      this.IntoUpdate({ id: record.id })
        .then((res) => this.updateSuccess(res, record.id))
        .catch(err => this.requestFailed(err))
    },
    updateSuccess (res, key) {
      if (res.status === 200 || res.status === '200') {
        this.$notification['success']({
          message: '成功',
          description: '删除成功',
          duration: 4
        })
        this.$refs.table.refresh()
      } else {
        this.requestFailed(res)
      }
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
